package org.kisekiproject.course.actions {
	import org.kisekiproject.actions.ActionController;
	import org.kisekiproject.course.CourseGroup;
	import org.kisekiproject.course.CoursePage;

	/**
	 * Navigation actions.
	 */
	public class NavigationActions extends BaseCourseActions {

		/**
		 * Register functions.
		 */
		override public function registerFunctions():void {
			_course.addAction(this,"show",["page"]);
			_course.addAction(this,"hide",["page"]);
			_course.addAction(this,"next",["group"]);
			_course.addAction(this,"previous",["group"]);
			_course.addAction(this,"first",["group"]);
			_course.addAction(this,"hideall",[]);
		}

		/**
		 * Show a page.
		 */
		public function show(page:String):void {
			_course.getPageById(page).visible=true;
			_course.notifyStateChange();
		}

		/**
		 * Hide a page.
		 */
		public function hide(page:String):void {
			var pages:Array= _course.getMatchingPages(page);

			if (pages.length==0)
				throw new Error("No matching pages: "+page);

			for each (var p:CoursePage in pages)
				p.visible=false;

			_course.notifyStateChange();
		}

		/**
		 * Show the first page in a group.
		 */
		public function first(groupId:String):void {
			_course.getGroupById(groupId).visibleIndex=0;
			_course.notifyStateChange();
		}

		/**
		 * Show the next page in a group.
		 */
		public function next(groupId:String):void {
			_course.getGroupById(groupId).visibleIndex++;
			_course.notifyStateChange();
		}

		/**
		 * Show the previous page in a group.
		 */
		public function previous(groupId:String):void {
			_course.getGroupById(groupId).visibleIndex--;
			_course.notifyStateChange();
		}

		/**
		 * Hide all pages.
		 */
		public function hideall():void {
			for each (var page:CoursePage in _course.pages)
				page.visible=false;

			_course.notifyStateChange();
		}
	}
}